package cn.org.cute.ebocy.pf;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import cn.org.cute.ebocy.config.Param;
import cn.org.cute.ebocy.pf.platformservices.PlatformServicesImpl;
import cn.org.cute.ebocy.util.NormalUtil;
import cn.org.cute.security.domain.PlatformUser;

/**
 * 平台相关的基本接口如获取用户，朋友等的信息 注入一个platformFaceServices,可使用
 * 
 * @Copyright©2011, 2011 EBO CHANGYOU Co.td. All rights reserved.
 * @Version 1.0.0
 * @since jdk1.6
 * @author xiaolei
 * @date 2011-12-13
 */
public class PlatformFaceServicesImpl {
	public static PlatformServicesImpl platformServices = null;
	private static PlatformFaceServicesImpl platformFaceServicesImpl = null;

	public static PlatformFaceServicesImpl getInstance() {
		if (platformServices == null) {
			platformServices = new PlatformServicesImpl();
		}
		if (platformFaceServicesImpl == null) {
			platformFaceServicesImpl = new PlatformFaceServicesImpl();
		}
		return platformFaceServicesImpl;
	}

	private PlatformFaceServicesImpl() {

	}

	/**
	 * 获取用户信息
	 * 
	 * @param paramsMap
	 *            参数
	 * @return PlatformUser 平台用户对象
	 */
	public PlatformUser getUserInfo(HashMap<String, String> paramsMap){
		try {
			return platformServices.getUsersProfile(paramsMap);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 获得当前登录用户平台ID
	 * 
	 * @param paramsMap
	 *            参数
	 * @return String 平台ID
	 */
	public String getLoggedInUser(HashMap<String, String> paramsMap){
		String uid = "0";
		uid = platformServices.getLoggedInUser(paramsMap);
		return uid;
	}

	/**
	 * 获得已添加应用好友列表
	 * 
	 * @param paramsMap
	 *            参数
	 * @return List<PlatformUser> 用户列表
	 */
	public String getAppFriends(HashMap<String, String> paramsMap){
		List<PlatformUser> appFriendsList = platformServices.getAppFriendsInfo(paramsMap);
		if (appFriendsList == null) {
			return null;
		}

		if (appFriendsList.size() == 0) {
			return "";
		}

		String appFriends = null;
		for (PlatformUser platformUser : appFriendsList) {
			if (appFriends == null) {
				appFriends = platformUser.getPfId();
			} else {
				appFriends += "," + platformUser.getPfId();
			}

		}

		return appFriends;

	}

	/**
	 * 获得平台好友信息
	 * 
	 * @param paramsMap
	 *            参数
	 * @return List<PlatformUser> 用户列表
	 */
	public List<PlatformUser> getFriends(HashMap<String, String> paramsMap){
		return null;
	}

	/**
	 * 获取好友的详细用户信息
	 * 
	 * @param paramsMap
	 *            参数
	 * @return List<PlatformUser> 用户列表
	 */
	public List<PlatformUser> getFriendUserInfos(HashMap<String, String> paramsMap, String fids){

		List<PlatformUser> list = null;
		PlatformUser platformUser = null;
		if ("1".equals(paramsMap.get(Param.ParamName.INFOED.getName()))) {
			paramsMap.put(Param.ParamName.ISFRIENDINFO.getName(), "1");
			paramsMap.put(Param.ParamName.UIDS.getName(), fids);
			list = platformServices.getUsersProfiles(paramsMap);
		} else {
			list = new ArrayList<PlatformUser>();
			for (String id : fids.split(",")) {
				platformUser = new PlatformUser();
				platformUser.setPfId(id);
				list.add(platformUser);
			}
		}

		return list;
	}

	/**
	 * 获取用户信息小于20个
	 * 
	 * @param paramsMap
	 *            参数
	 * @return List<PlatformUser> 用户列表
	 */
	public List<PlatformUser> getUserInfos(HashMap<String, String> paramsMap) {
		List<PlatformUser> list = null;
		list = platformServices.getUsersProfiles(paramsMap);
		return list;
	}

	/**
	 * 判断用户是否已经对应用授权
	 * 
	 * @param paramsMap
	 *            参数
	 * @return boolean
	 * @throws Exception 
	 */
	public boolean getIsAppUser(HashMap<String, String> paramsMap) throws Exception {
		boolean isAppUser = false;
		// 如果uid为null或uid为“0”则取当前用户uid
		String uid = paramsMap.get(Param.ParamName.UID.getName());
		if (NormalUtil.isNull(uid) || "0".equals(uid)) {
			uid = platformServices.getLoggedInUser(paramsMap);
			paramsMap.put(Param.ParamName.UID.getName(), uid);
		}

		isAppUser = platformServices.getIsAppUser(paramsMap);
		return isAppUser;
	}

	/**
	 * 判断是否是当前用户好友
	 * 
	 * @param paramsMap
	 *            参数
	 * @return boolean
	 * @throws Exception 
	 */
	public boolean getIsFriend(HashMap<String, String> paramsMap) throws Exception {
		boolean isFriend = false;
		// 如果uid为null或uid为“0”则取当前用户uid
		String uid = paramsMap.get(Param.ParamName.UID.getName());
		if (NormalUtil.isNull(uid) || "0".equals(uid)) {
			uid = platformServices.getLoggedInUser(paramsMap);
			paramsMap.put(Param.ParamName.UID.getName(), uid);
		}

		isFriend = platformServices.getIsFriend(paramsMap);
		return isFriend;
	}

	/**
	 * 判断是否是当前用户好友
	 * 
	 * @param paramsMap
	 *            参数
	 * @return boolean
	 * @throws Exception 
	 */
	public String getIsVip(HashMap<String, String> paramsMap) throws Exception{
		String isFriend = "0,0,0";
		// 如果uid为null或uid为“0”则取当前用户uid
		String uid = paramsMap.get(Param.ParamName.UID.getName());
		if (NormalUtil.isNull(uid) || "0".equals(uid)) {
			uid = platformServices.getLoggedInUser(paramsMap);
			paramsMap.put(Param.ParamName.UID.getName(), uid);
		}

		isFriend = platformServices.getIsVip(paramsMap);
		return isFriend;
	}

	/**
	 * 判断是否是当前用户好友
	 * 
	 * @param paramsMap
	 *            参数
	 * @return boolean
	 * @throws Exception 
	 */
	public boolean getIsLogin(HashMap<String, String> paramsMap) throws Exception{
		boolean isLogin = false;
		// 如果uid为null或uid为“0”则取当前用户uid
		String uid = paramsMap.get(Param.ParamName.UID.getName());
		if (NormalUtil.isNull(uid) || "0".equals(uid)) {
			uid = this.platformServices.getLoggedInUser(paramsMap);
			paramsMap.put(Param.ParamName.UID.getName(), uid);
		}

		isLogin = this.platformServices.getIsLogin(paramsMap);
		return isLogin;
	}

	/**
	 * 判断是否为页面粉丝
	 * 
	 * @param paramsMap
	 *            参数
	 * @return boolean
	 */
	public boolean getIsFan(HashMap<String, String> paramsMap){
		boolean isFan = false;
		String uid = paramsMap.get(Param.ParamName.UID.getName());
		// 如果uid为null或uid为“0”则取当前用户uid
		if (NormalUtil.isNull(uid) || "0".equals(uid)) {
			uid = this.platformServices.getLoggedInUser(paramsMap);
			paramsMap.put(Param.ParamName.UID.getName(), uid);
		}
		isFan = this.platformServices.page_isFans(paramsMap);
		return isFan;
	}

	/**
	 * 过滤敏感关键词
	 * 
	 * @param paramsMap
	 *            参数
	 * @return boolean
	 * @throws Exception 
	 */
	public String getFilterWord(HashMap<String, String> paramsMap) throws Exception {
		String content = null;
		String uid = paramsMap.get(Param.ParamName.UID.getName());
		// 如果uid为null或uid为“0”则取当前用户uid
		if (NormalUtil.isNull(uid) || "0".equals(uid)) {
			uid = this.platformServices.getLoggedInUser(paramsMap);
			paramsMap.put(Param.ParamName.UID.getName(), uid);
		}
		content = this.platformServices.getFilterWord(paramsMap);
		return content;
	}

	/**
	 * 支付接口（qq）
	 * 
	 * @param paramsMap
	 *            参数
	 * @return boolean
	 */
	public String buyGoods(HashMap<String, String> paramsMap) throws Exception {
		String content = null;
		String uid = paramsMap.get(Param.ParamName.UID.getName());
		// 如果uid为null或uid为“0”则取当前用户uid
		if (NormalUtil.isNull(uid) || "0".equals(uid)) {
			uid = this.platformServices.getLoggedInUser(paramsMap);
			paramsMap.put(Param.ParamName.UID.getName(), uid);
		}
		content = this.platformServices.buyGoods(paramsMap);
		return content;
	}

	public static void main(String[] args) {
		// 调用范例
		PlatformFaceServicesImpl platformFaceServices = PlatformFaceServicesImpl.getInstance();
		String pfid = "328870356";
		String key = "2.d4706345405a9a7933ca446f4e4551b4.3600.1339045200-328870356";

		HashMap<String, String> params = new HashMap<String, String>();
		params.put(Param.ParamName.UID.getName(), pfid);// 平台ID
		params.put(Param.ParamName.AUTH_TOKEN.getName(), key);// session_key
		try {
			PlatformUser pu =platformFaceServices.getUserInfo(params);// 用户平台信息
			System.out.println(pu.getPfUptime());
			// platformFaceServices.getAppFriends(params);//
			// 安装了游戏的好友列表，如11,22,33
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
